﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VLUSM_DataAccess.Repositories
{
    public class UserRepository : GenericRepository<MainDataContainer>
    {
        public UserRepository(string connectionString) : base(connectionString, "MainData") { }
        public UserRepository(MainDataContainer context) : base(context) { }

        public User GetSingleByUserNamePassword(string username, string password)
        {
            var query = this.GetSingleOrDefault<User>(u => u.TenTaiKhoan == username && u.MatKhau == password);
            return query;
        }

        public User GetSingleByID(int IDFilter)
        {
            var query = this.GetSingle<User>(u => u.ID == IDFilter);
            return query;
        }

        public User GetSingleByTenTaiKhoan(string TenTaiKhoanFilter)
        {
            var query = this.GetSingle<User>(u => u.TenTaiKhoan == TenTaiKhoanFilter);
            return query;
        }

        public IEnumerable<User> GetAll()
        {
            var query = this.GetAll<User>();
            return query;
        }

        public IEnumerable<User> GetAllByDonViID(int DonViIDFilter)
        {
            var query = this.Find<User>(u => u.DonViID == DonViIDFilter);
            return query;
        }

        public void Save(User obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<User>(obj);
                }
                else
                {
                    this.Add<User>(obj);
                }
            }
            catch { }
        }

        public void Remove(User obj)
        {
            this.Delete<User>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingle<User>(d => d.ID == IDFilter);
            this.Delete<User>(obj);
        }
    }
}
